package com.psa.services;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.psa.tool.SystemStatus;

/**
 * Created by david on 2014-10-17.
 */
public class TestService extends Service {
    private IBinder binder = new MyBinder();
    private boolean running = false;
    private String msg = "";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(SystemStatus.TAG, "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(SystemStatus.TAG, "onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.i(SystemStatus.TAG, "onRebind");
        super.onRebind(intent);
    }

    @Override
    public void onDestroy() {
        Log.i(SystemStatus.TAG, "onDestroy");
        super.onDestroy();
    }

    public boolean isRunning(){
        return running;
    }

    public void start(String txt){
        msg = txt;
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                running = true;
                while (running) {
                    Log.i(SystemStatus.TAG, msg + "(time:"+(i*2)+")");
                    try {
                        Thread.sleep(500);
                        i++;
                    } catch (Exception e) {
                        Log.e(SystemStatus.TAG, e.getMessage());
                    }
                }
            }
        }).start();
    }

    public void stop() {
        Log.i(SystemStatus.TAG, "stop");
        running = false;
    }

    public class MyBinder extends Binder{
        public TestService getService(){
            return TestService.this;
        }
    }
}
